﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;
using Hockey.Shared;
using System.Windows;

namespace Hockey.UILayer.ViewModels
{
    /// <summary>
    /// ViewModel for enter player form 
    /// </summary>
    public class PlayerNameFormVM: NotificationObject
    {
        private IEventAggregator _events;

        public PlayerNameFormVM(IEventAggregator events)
        {
            if(events == null)
                throw new ArgumentNullException("Events");
            _events = events;

            Success = false;

            // create commands
            OKCommand = new AutoDelegateCommand(() => { OKHandler(); });
            CancelCommand = new AutoDelegateCommand(() => { CancelHandler(); });
        }

        #region Binding Properties

        public string PlayerName
        { get; set; }

        public bool Success
        { get; set; }

        public AutoDelegateCommand OKCommand
        { get; set; }

        public AutoDelegateCommand CancelCommand
        { get; set; }

        #endregion

        /// <summary>
        /// Notifes, player name successfully entered
        /// </summary>
        private void OKHandler()
        {
            // validate player;s name
            if (string.IsNullOrWhiteSpace(PlayerName))
            {
                MessageBox.Show("Zadejte prosím jméno hráče.");
                return;
            }

            Success = true;
            CloseForm();
        }

        /// <summary>
        /// Cancels the form
        /// </summary>
        private void CancelHandler()
        {
            Success = false;
            CloseForm();
        }

        /// <summary>
        /// Notifies view to close
        /// </summary>
        private void CloseForm()
        {
            _events.GetEvent<CloseForm>().Publish(this);
        }

    }
}
